//
//  ZWLMainViewController.m
//  zwlzhibo
//
//  Created by czh on 16/9/2.
//  Copyright © 2016年 czh. All rights reserved.
//

#import "ZWLMainViewController.h"
#import "ZWLLiveCell.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "ZWLItemModel.h"
#import "ZWLLiveViewController.h"


static NSString * const cellID = @"zhibocell";

@interface ZWLMainViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *mytableView;

/** 直播数据 */
@property (strong, nonatomic) NSMutableArray *lives;

@end

@implementation ZWLMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupTableView];
    
    [self loadData];
}


#pragma mark - 加载数据
- (void)loadData
{
    // 映客数据url
    NSString *urlStr = @"http://116.211.167.106/api/live/aggregation?uid=133825214&interest=1";
    
    // 请求数据
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFJSONResponseSerializer serializer];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", nil];
    [mgr GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary * _Nullable responseObject) {
        
        _lives = [ZWLItemModel mj_objectArrayWithKeyValuesArray:responseObject[@"lives"]];
        
        [_mytableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
    
}

#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _lives.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZWLLiveCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.live = _lives[indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZWLLiveViewController *vc = [[ZWLLiveViewController alloc] init];
    vc.live = _lives[indexPath.row];
    [self presentViewController:vc animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 430;
}


#pragma mark - 布局UI
- (void)setupTableView
{
    self.navigationItem.title = @"直播列表";
    [_mytableView registerNib:[UINib nibWithNibName:@"ZWLLiveCell" bundle:nil] forCellReuseIdentifier:cellID];
    
    _mytableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}



@end
